package cn.edu.nju.ws.sview.database;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;


public  class DBConnectionFactory {
	public static int STRING_MAX_LENGTH = 65535;
	private DBConnectionFactory(){
		//Nothing
	}
	private static ComboPooledDataSource datasource = null;
	private static ComboPooledDataSource falconDatasource = null;
	private static void initDataSource(){
		try {
			Properties properties = new Properties();
			properties.load(DBConnectionFactory.class.getClassLoader().getResourceAsStream("conf.properties"));
			datasource = new ComboPooledDataSource();
			datasource.setDriverClass("com.mysql.jdbc.Driver");
			datasource.setJdbcUrl(properties
					.getProperty("db_connection_url"));
			datasource.setUser(properties
					.getProperty("db_connection_user"));
			datasource.setPassword(properties
					.getProperty("db_connection_password"));
			datasource.setMinPoolSize(10);
			datasource.setIdleConnectionTestPeriod(600);
			properties.clear();
			properties = null;
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void initFalconDataSource(){
		try {
			Properties properties = new Properties();
			properties.load(DBConnectionFactory.class.getClassLoader().getResourceAsStream("conf.properties"));
			falconDatasource = new ComboPooledDataSource();
			falconDatasource.setDriverClass("com.mysql.jdbc.Driver");
			falconDatasource.setJdbcUrl(properties
					.getProperty("falcon_connection_url"));
			falconDatasource.setUser(properties
					.getProperty("falcon_connection_user"));
			falconDatasource.setPassword(properties
					.getProperty("falcon_connection_password"));
			falconDatasource.setMaxPoolSize(20);
			falconDatasource.setMinPoolSize(10);
			falconDatasource.setIdleConnectionTestPeriod(600);
			properties.clear();
			properties = null;
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
	
	public  synchronized  static Connection getConnection(){
		Connection con= null;
		try {
			if(datasource==null)
				initDataSource();
			con = datasource.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}

	public  synchronized  static Connection getFalconConnection(){
		Connection con= null;
		try {
			if(falconDatasource==null)
				initFalconDataSource();
			con = falconDatasource.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}
	/*//s====================
	private static ComboPooledDataSource tdatasource = null;
	private static void initTDataSource(){
		try {
			Properties properties = new Properties();
			properties.load(DBConnectionFactory.class.getClassLoader().getResourceAsStream("conf.properties"));
			tdatasource = new ComboPooledDataSource();
			tdatasource.setDriverClass("com.mysql.jdbc.Driver");
			tdatasource.setJdbcUrl(properties
					.getProperty("db_connection_url"));
			tdatasource.setUser(properties
					.getProperty("db_connection_user"));
			tdatasource.setPassword(properties
					.getProperty("db_connection_password"));
			tdatasource.setMaxPoolSize(20);
			tdatasource.setMinPoolSize(10);
			tdatasource.setIdleConnectionTestPeriod(600);
			properties.clear();
			properties = null;
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public  synchronized  static Connection getTConnection(){
		Connection con= null;
		try {
			if(tdatasource==null)
				initTDataSource();
			con = tdatasource.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}

	//e====================
*/	
	public static synchronized void finalized(){
		if(datasource!=null){
			datasource.close();
			datasource = null;
		}
		
		if(falconDatasource!=null){
			falconDatasource.close();
			falconDatasource = null;
		}
		/*if(tdatasource!=null){
			tdatasource.close();
			tdatasource = null;
		}*/
		
	}
}
